{% extends "shm/base.html" %}
{% block extrastyle %}<link rel="stylesheet" type="text/css" href="{% load media %}{% media_prefix %}css/sklad-tree.css">{% endblock %}
{% block department_tree %}

<script type="text/javascript" src="{% media_prefix %}js/phones.js"></script>
<script type="text/javascript" src="{% media_prefix %}js/jquery.form.min.js"></script>

<link rel="stylesheet" type="text/css" href="{% load media %}{% media_prefix %}css/phones.css">

       
<script type="text/javascript">
function GetCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" +
        expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}


function LoadTreeNode( node_id ){
    id = node_id.replace('as_','');
    if($('#uls_'+id).text() == "") {
        $('#uls_'+id).load('/shm/department/'+id+'/');   
    }else{
       $('#uls_'+id).show();
    }
    $('#'+node_id+' img:first').attr("src","media/img/minus.gif");
    return true;
}


function UnHideOpenNodes(){
    var sOpenNode = GetCookie('opennode'); 
    if (sOpenNode != null){
        var aOpenNode = new Array();
        aOpenNode = sOpenNode.split(';');
        for(i=0;i<(aOpenNode.length-1);i++)
        {
            LoadTreeNode(aOpenNode[i]);
        }
    }
};



function Unhide(e, id){
    img_pm = e.getElementsByTagName('img')[0];
    if( img_pm.src.indexOf('plus') +1 ){
        //img_pm.src = 'media/img/minus.gif';
        //if($('#uls_'+id).text() == "") {
        //    $('#uls_'+id).load('/shm/department/'+id+'/');               
        //}else{
        //    $('#uls_'+id).show();
        //}

        LoadTreeNode(e.id);
        
        OpenNode = GetCookie('opennode');
        if(OpenNode != null) SetCookie('opennode', OpenNode + e.id + ';')
        else SetCookie('opennode', e.id + ';'); 
        
    }else{   
        img_pm.src = 'media/img/plus.gif';
        $('#uls_'+id).hide();
        
        OpenNode = GetCookie('opennode');
        if(OpenNode != null){
            var b = OpenNode.indexOf(e.id)
            if (b != -1){
                var sa = OpenNode.slice(0,b);
                var sb = OpenNode.slice(b+e.id.length+1);
                SetCookie('opennode', sa + sb);
            }
        }
    }
    return false;
};

function get_dept(id){
    $('#department_detail').load('/shm/department/detail/'+id+'/'); 
    return false;
};

//setTimeout(UnHideOpenNodes, 200);


//$(document).ready(
//    //Set UnHideOpenNodes()
//    setTimeout(UnHideOpenNodes, 500)
//    );
</script>


    <h3>Отделы</h3> 
    <p><a href="">Добавить отдел</a></p>
    <div class="treeview">
    <ul>
        {% include "shm/tree_item.html" %}
    </ul>
    </div>
 

  
{% endblock %}
